using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SimExpApp
{
    public partial class MachineTable : UserControl
    {
        class Column
        {
            public Label Text;
            public List<TextBox> Rows;

            public Column(string text)
            {
                Rows = new List<TextBox>();
                Text = new Label();
                Text.Text = text;
                Text.TextAlign = ContentAlignment.MiddleCenter;
                Text.AutoSize = false;
            }

            public TextBox InsertRow(int i, string text)
            {
                TextBox tb = new TextBox();
                tb.Text = text;
                tb.ReadOnly = true;
                tb.TextAlign =  HorizontalAlignment.Center;
                //tb.DropDownStyle = TextBoxStyle.DropDownList;
                Rows.Insert(i, tb);
                return tb;
            }
        }

        List<Column> columns = new List<Column>();
        List<Label> rows = new List<Label>();

        public MachineTable()
        {
            InitializeComponent();
        }

        public void RemoveAll()
        {
            foreach (Control ctrl in rows) Controls.Remove(ctrl);
            foreach (Column c in columns)
            {
                Controls.Remove(c.Text);
                foreach (Control ctrl in c.Rows) Controls.Remove(ctrl);
            }
            rows.Clear();
            columns.Clear();
        }

        public void InsertColumn(int i, string title)
        {
            Column newcol = new Column(title);
            for (int j = 0; j < rows.Count; j++) newcol.InsertRow(0, "");
            columns.Insert(i, newcol);
            Rebuild();
        }

        public void AddColumn(string title)
        {
            InsertColumn(columns.Count, title);
        }

        public void InsertRow(int i, string title)
        {
            Label l = new Label();
            l.Text = title;
            rows.Insert(i, l);
            foreach (Column c in columns)
            {
                c.InsertRow(i, "");
            }
            Rebuild();
        }

        public void AddRow(string title)
        {
            InsertRow(rows.Count, title);
        }

        public void RemoveColumn(int i)
        {
            Column c = columns[i];
            Controls.Remove(c.Text);
            foreach (Control ctrl in c.Rows) Controls.Remove(ctrl);
            columns.Remove(c);
            Rebuild();
        }

        public void RemoveRow(int i)
        {
            Controls.Remove(rows[i]);
            rows.RemoveAt(i);
            foreach (Column c in columns)
            {
                Controls.Remove(c.Rows[i]);
                c.Rows.RemoveAt(i);
            }
            Rebuild();
        }

        public void Rebuild()
        {
            const int Dy = 24;
            const int Dx = 50;
            int tab_index = 0;

            for (int i = 0; i < rows.Count; i++)
            {
                if (!Controls.Contains(rows[i]))
                    Controls.Add(rows[i]);
                rows[i].AutoSize = true;
                rows[i].Location = new System.Drawing.Point(3 - HorizontalScroll.Value, Dy + Dy * i + 3 - VerticalScroll.Value);
            }
            for (int j = 0; j < columns.Count; j++)
            {
                Column c = columns[j];
                if (!Controls.Contains(c.Text))
                    Controls.Add(c.Text);

                c.Text.Left = Dx + Dx * j - HorizontalScroll.Value;
                c.Text.Top = 3 - VerticalScroll.Value;
                c.Text.Width = Dx - 2;
                c.Text.Height = Dy - 4;

                for (int i = 0; i < rows.Count; i++)
                {
                    if (!Controls.Contains(c.Rows[i])) Controls.Add(c.Rows[i]);
                    c.Rows[i].Left = Dx + Dx * j + 1 - HorizontalScroll.Value;
                    c.Rows[i].Top = Dy + Dy * i - VerticalScroll.Value;
                    c.Rows[i].Width = Dx - 2;
                    c.Rows[i].Height = Dy - 4;
                    c.Rows[i].TabIndex = tab_index;
                    tab_index++;
                }
            }
        }

        public TextBox this[int x, int y]
        {
            get
            {
                return columns[x].Rows[y];
            }
        }

        public int ColumnCount
        {
            get { return columns.Count; }
        }

        public int RowCount
        {
            get { return rows.Count; }
        }

        public Label GetColumnTitle(int i)
        {
            return columns[i].Text;
        }

        public Label GetRowTitle(int i)
        {
            return rows[i];
        }

        public TextBox Selected
        {
            get
            {
                foreach (Control c in Controls)
                    if (c.Focused) return (TextBox)c;
                return null;
            }
            set
            {
                if (Controls.Contains(value))
                    value.Focus();
            }
        }

        public int SelectedRow
        {
            get
            {
                for (int i = 0; i < rows.Count; i++)
                {
                    for (int j = 0; j < columns.Count; j++)
                        if (columns[j].Rows[i].Focused) return i;
                }
                return -1;
            }
        }

        public int SelectedColumn
        {
            get
            {
                for (int j = 0; j < columns.Count; j++)
                {
                    foreach (Control c in columns[j].Rows)
                        if (c.Focused) return j;
                }
                return -1;
            }
        }
    }
}
